package net.onceface.web.resources;

import java.util.Date;

import javax.inject.Inject;
import javax.ws.rs.BeanParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import net.onceface.application.ClientApplication;
import net.onceface.application.ClientProtectInfoApplication;
import net.onceface.application.assembler.ClientAssembler;
import net.onceface.application.commons.InvokeResult;
import net.onceface.application.commons.Message;
import net.onceface.application.dto.ClientDTO;
import net.onceface.application.dto.PersonDTO;
import net.onceface.infra.json.JsonUtil;
import net.onceface.infra.util.Strings;


@Path("/client")
public class ClientResource extends BaseResource{

	@Inject
	private ClientApplication application;
	
	@Inject
	private ClientProtectInfoApplication protectInfoApplication;
	
	/**
	 * 按用户id获取用户信息 
	 * **/
	@GET
	@Path("/getClient")
	@Produces({ MediaType.APPLICATION_JSON })
	@Consumes(MediaType.APPLICATION_JSON)  
	public String getUser(@QueryParam("clientId") final Long clientId){
		try{
			return toDateJson(InvokeResult.success(application.getClient(clientId)));
		}catch(Exception e){
			return JsonUtil.objectToJson(InvokeResult.failure(Message.GET_CLIENT_FAILURE));
		}
	}
	
	/**
	 * 按用户账户获取用户信息 
	 * **/
	@GET
	@Path("/getClientByAccount")
	@Produces({ MediaType.APPLICATION_JSON })
	@Consumes(MediaType.APPLICATION_JSON)
	public String getDataAccess(@QueryParam("userAccount")final String userAccount){
		try{
			return toDateJson(application.findClientByAccount(userAccount));
		}catch(Exception e){
			return JsonUtil.objectToJson(InvokeResult.failure(Message.GET_CLIENT_FAILURE));
		}
	}
	
	/**
	 * 按客户ID获取客户重要信息
	 * **/
	@GET
	@Path("/getClientProtectInfo")
	@Produces({ MediaType.APPLICATION_JSON })
	@Consumes(MediaType.APPLICATION_JSON)
	public String getDataAccess(@QueryParam("clientId")final Long clientId){
		try{
			return toDateJson(protectInfoApplication.findProtectInfoByClientId(clientId));
		}catch(Exception e){
			return JsonUtil.objectToJson(InvokeResult.failure(Message.GET_CLIENT_FAILURE));
		}
	}
	
	/**
	 * 更新用户
	 * **/
	@POST
	@Path("/updateClient")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	public InvokeResult updateUser(@BeanParam ClientDTO clientDTO){
		try{
			ClientDTO clientI = application.getClient(clientDTO.getId());
			ClientDTO clientII = (ClientDTO) clientI.clone();
			if(!Strings.isEmpty(clientDTO.getNick())){
				clientII.setNick(clientDTO.getNick());
			}
			if(!clientI.equals(clientII)){
				application.updateClient(clientII);
			}
			return InvokeResult.success();
		}catch(Exception e){
			return InvokeResult.failure("更新用户信息失败!");
		}
	}
	
	/**
	 * 用户更新头像
	 * **//*
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Path("/updateAvatar")
	@Consumes({MediaType.APPLICATION_FORM_URLENCODED,MediaType.APPLICATION_JSON})
	public InvokeResult updateAvatar(@FormParam("id")long id, @FormParam("avatar")String avatar){
		return userApplication.updateAvatar(id, avatar);
	}*/
}
